<?php
/* mycurator_cloud_pgworker
 * This file contains the code to read the wp_cs_pageq table and fetch pages from diffbot.
 * Pages fetched successfully are stored in wp_cs_cache - the pageq entry is deleted in all cases.
 * This version is for sequential processing and expects to be started by Cron.  It will stop if another version is running.
*/

//error reporting
error_reporting(E_ERROR);
//File definitions
$fpath = '/var/log/tgtinfo.net/';      //  /var/log/tgtinfo.net/';
define('PIDFILE', $fpath.'myfile.pid');
define('LOGFILE', $fpath.'page_log');
//Constants for the log
define ('MCT_AI_LOG_ERROR','ERROR');
define ('MAX_ERROR',2);
define ('MAX_TRY',1);
define ('MCT_AI_LOG_ACTIVITY','ACTIVITY');
define ('MCT_AI_LOG_PROCESS','PROCESS');
define ('MAX_READ',50);
//Set timezone
date_default_timezone_set("America/New_York");
//Open log
global $flog, $argv;
$flog = fopen(LOGFILE,'a');
mct_cs_log('Page Worker',MCT_AI_LOG_PROCESS,"Server Starting",$argv[1]);
//Check if another copy is running
if (file_exists(PIDFILE)) {
    $pid = file_get_contents(PIDFILE);
    $kill = shell_exec('kill -0 '.$pid.' 2>&1');
    //mct_cs_log(MCT_AI_LOG_PROCESS,"Server Already Running, Kill Return",$kill);
    if (!$kill) {
        mct_cs_log('Page Worker',MCT_AI_LOG_PROCESS,"Server Already Running, Stopping",$pid);
        fclose($flog);
        exit();  //ping the process and exit if it is still running
    }
}
//Set pidfile to say we are running
file_put_contents(PIDFILE, getmypid());
function removePidFile() {
    unlink(PIDFILE);
}
register_shutdown_function('removePidFile');   
//Get the support functions
require_once('mycurator_cloud_fcns.php');
//database globals
//
$cs_server = 'localhost';
$cs_user = 'tgtai';
$cs_pwd = '7724@TI';
$cs_db = 'target-info';
/*
$cs_server = 'localhost';
$cs_user = 'memepost';  //root
$cs_pwd = '669728@Mt';  //''
$cs_db = 'memepost_wrd6'; //wp_tgtinfo
*/
//Connect to the DB
global $dblink;
$dblink = mysqli_connect($cs_server,$cs_user, $cs_pwd, $cs_db);
if (mysqli_connect_error()) {
    mct_cs_log('Page Worker',MCT_AI_LOG_ERROR, 'Service Could Not Connect to DB',mysqli_connect_error());
    mct_cs_closeout();
    exit();
}
// Ready to go
//mct_cs_log(MCT_AI_LOG_PROCESS,"Server Up",'');
//Get count to read from back
$sql = 'SELECT count(*) as cnt FROM `wp_cs_requests`';
$sql_result = mysqli_query($dblink, $sql);
$row = mysqli_fetch_assoc($sql_result);
if ($row['cnt'] > MAX_READ) {
    $lim = $row['cnt'] - MAX_READ;
    $limit = " LIMIT $lim,".MAX_READ;
} else {
    $limit = " LIMIT ".MAX_READ;
}
mysqli_free_result($sql_result);
//Query the pageq table
$sql = 'SELECT * FROM `wp_cs_requests` '.$limit;
$sql_result = mysqli_query($dblink, $sql);
if (!$sql_result){
    mct_cs_log('Page Worker',MCT_AI_LOG_ERROR, "DB Error Couldn't Select",'');
    mct_cs_closeout();
    exit();
}
//read and process
while ($row = mysqli_fetch_assoc($sql_result)){
    $page = '';
    if ($row['rq_err_try'] >= MAX_TRY && $row['rq_errcnt'] >= MAX_ERROR ) {
        //Don't try to get page again, just drop through and move it to cache as an empty error page
    } else {
        if ($row['rq_errcnt'] >= MAX_ERROR) continue; //quit trying to process 
        //Check if page is already in cache - non-request sites could cause an insert
        $cache_url = mysqli_real_escape_string($dblink, $row['rq_url']);
        $chksql = "SELECT `pr_id`
            FROM wp_cs_cache 
            WHERE pr_url = '$cache_url'";
        $chk_result = mysqli_query($dblink,$chksql);
        if ($chk_result && mysqli_num_rows($dblink,$chk_result) == 1){
            mct_cs_delrqst($row['rq_id']);  //delete this entry and continue
            mct_cs_log('Page Worker',MCT_AI_LOG_ACTIVITY, "Page Already in Cache",$row['rq_url']);
            continue;
        }
        //Now get page from diffbot
        $page = mct_ai_call_diffbot($row['rq_url'], array('topic_name' => 'Page Worker'));
        if (empty($page)) {
            $ecnt = $row['rq_errcnt'] + 1;
            $sql = "UPDATE wp_cs_requests SET rq_errcnt = $ecnt WHERE rq_id = ".$row['rq_id'];
            $upd_result = mysqli_query($dblink, $sql);
            if (!$upd_result){
                mct_cs_log('Page Worker',MCT_AI_LOG_ERROR, "DB Error Couldn't Update Error Count",'');
            }
            continue;  //forget errors for now - we'll try again later
        } 
    } 
    //Check if page is already in cache - non-request sites could cause an insert
    $sql_url = mysqli_real_escape_string($dblink, $row['rq_url']);
    $chksql = "SELECT *
        FROM wp_cs_cache 
        WHERE pr_url = '$sql_url'";
    $chk_result = mysqli_query($dblink,$chksql);
    if ($chk_result && mysqli_num_rows($dblink,$chk_result) == 1){
        mct_cs_delrqst($row['rq_id']);  //delete this entry and continue
        mct_cs_log('Page Worker',MCT_AI_LOG_ACTIVITY, "Page Already in Cache",$row['rq_url']);
        continue;
    }
    //Insert to cache
    $sql_page = mysqli_real_escape_string($dblink, $page);
    $sql = "INSERT INTO wp_cs_cache (pr_page_content, pr_usage, pr_url) VALUES ('$sql_page',0,'$sql_url')";
    $ins_result = mysqli_query($dblink, $sql);
    if (!$ins_result){
        mct_cs_log('Page Worker',MCT_AI_LOG_ERROR, "DB Error Couldn't Insert to Cache",'');
        continue; //Leave page in requets for next time
    }
    mct_cs_delrqst($row['rq_id']);
    
    mct_cs_log('Page Worker',MCT_AI_LOG_ACTIVITY, "Page Loaded in Cache",$row['rq_url']);
}
mysqli_free_result($sql_result);
mct_cs_log('Page Worker',MCT_AI_LOG_PROCESS,"Server Shutting Down",'');
mct_cs_closeout();
exit();
//
//Functions below
//
function mct_cs_closeout() {
    //Close out log, database
    global $flog, $dblink;
    
    fclose($flog);
    mysqli_close($dblink);
}

function mct_cs_log($name,$type,$msg,$val){
    //log activity to page_log
    global $flog;
    
    $ts = date('m d y g:i:s');
    fwrite($flog,"$ts $name $type '$msg' '$val' \n");
}

function mct_cs_delrqst($del) {
    global $dblink;
    
    $sql = "DELETE FROM wp_cs_requests WHERE rq_id = ".$del;
    $sql_result = mysqli_query($dblink, $sql);
    if (!$sql_result){
        mct_cs_log('Page Worker',MCT_AI_LOG_ERROR, "DB Error Couldn't Remove from Requests",$del);
    }
}

?>